<template>
  <div class="login_page">
    <van-nav-bar>
      <template #left>
        <van-icon @click="close" class="cha" name="cross" size="22" />
      </template>
    </van-nav-bar>
    <div class="content">
      <div class="title">欢迎登录Hello</div>
      <van-field
        v-model="tel"
        center
        clearable
        type="tel"
        placeholder="请输入手机号"
      >
      </van-field>
      <van-field
        v-model="password"
        :type="leixing"
        center
        clearable
        placeholder="请输入密码"
      >
        <template #right-icon>
          <van-icon
            v-if="leixing == 'password'"
            @click="qiehuan_status"
            name="closed-eye"
          />
          <van-icon v-else @click="qiehuan_status" name="eye-o" />
        </template>
      </van-field>
      <div class="xieyi">
        <input type="checkbox" @change="status" />
        <div class="zi">
          我已阅读并同意《Hello用户协议》、《隐私政策》，并授权Hello使用该Hello账号信息（如昵称、头像、收货地址）进行统一管理。
        </div>
      </div>
      <van-button
        @click="login"
        :class="login_class"
        :disabled="disabled"
        type="default"
        >登录</van-button
      >
      <van-button @click="zhuce" class="zhuce" type="default">注册</van-button>
    </div>
  </div>
</template>

<script>
import { Toast } from 'vant'
export default {
  name: "login",
  data() {
    return {
      tel: 18613719915,
      password: 12345678,
      leixing: "password",
      login_class: "login_btn1",
      disabled: true,
    };
  },
  methods: {
    close() {
      this.$router.go(-1);
    },
    qiehuan_status() {
      if (this.leixing == "password") {
        this.leixing = "number";
      } else {
        this.leixing = "password";
      }
    },
    status(e) {
      if (e.target.checked == true) {
        this.login_class = "login_btn2";
        this.disabled = false;
      } else {
        this.login_class = "login_btn1";
        this.disabled = true;
      }
    },
    yanzheng() {
      var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
      if (!myreg.test(this.tel)) {
        return false;
      }
    },
    login() {
      // 手机号验证
      if (this.yanzheng() == false) {
        Toast.fail('请输入正确的手机号');
        return 
      }
      if(this.password.length <= 0) {
        Toast.fail('密码不能为空');
        return
      }

      let tmp_obj = {
        tel: this.tel,
        password: this.password,
      };
      this.axios({
        method: "post",
        url: "http://localhost:3000/login",
        data: tmp_obj,
      })
        .then((res) => {
          console.log(res.data.data[0]);
          let waimai = res.data.data[0]
          if (res.data.msg == "登陆成功") {
            Toast.success(res.data.msg);
            localStorage.setItem("waimai", JSON.stringify(waimai))
            this.$router.replace({
              name:"user",
              
            })
          } else {
            Toast.fail(res.data.msg);
          }
          
        })
        .catch((err) => {
          console.log(err);
        });
    },
    zhuce() {
      this.$router.push("/register");
    },
  },
  created() {
    if (this.$route.params) {
      var a = this.$route.params
      this.tel = a.tel
      this.password = a.password
    }
  }
};
</script>

<style scoped>
.login_page {
  height: 100vh;
  background-color: white;
}

.cha {
  color: black;
}

.content {
  padding: 50px;
}

.title {
  margin-bottom: 20px;
  font-size: 30px;
}

.van-cell {
  padding: 0px;
  height: 50px;
  font-size: 18px;
  border-bottom: 1px solid grey;
}

/deep/.van-field__control {
  height: 50px;
}

.xieyi {
  display: flex;
  margin-top: 30px;
}

.zi {
  font-size: 11px;
}

.login_btn1 {
  height: 40px;
  background-color: rgb(247, 247, 193);
  margin-top: 10px;
  border-radius: 3px;
  width: 100%;
}

.login_btn2 {
  height: 40px;
  background-color: rgb(218, 218, 9);
  margin-top: 10px;
  border-radius: 3px;
  width: 100%;
}

.zhuce {
  width: 100%;
  margin-top: 10px;
  border: 1px solid rgb(247, 247, 193);
}
</style>